package com.ruoyi.web.controller.mxerp;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.mapper.MxProductImgMapper;
import com.ruoyi.system.service.MxProductImgService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MxProductImg;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品图片Controller
 *
 * @author yushuaixm
 * @date 2024-08-22
 */
@RestController
@RequestMapping("/mxerp/productimg")
public class MxProductImgController extends BaseController {
    @Autowired
    private MxProductImgService mxProductImgService;


    @Autowired
    private MxProductImgMapper mxProductImgMapper;

    /**
     * 查询商品图片列表
     */
    @PreAuthorize("@ss.hasPermi('mxerp:productimg:list')")
    @GetMapping("/list")
    public TableDataInfo list(MxProductImg mxProductImg) {
        startPage();
        List<MxProductImg> list = mxProductImgService.selectMxProductImgList(mxProductImg);
        return getDataTable(list);
    }

    /**
     * 导出商品图片列表
     */
    @PreAuthorize("@ss.hasPermi('mxerp:productimg:export')")
    @Log(title = "商品图片", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MxProductImg mxProductImg) {
        List<MxProductImg> list = mxProductImgService.selectMxProductImgList(mxProductImg);
        ExcelUtil<MxProductImg> util = new ExcelUtil<MxProductImg>(MxProductImg.class);
        util.exportExcel(response, list, "商品图片数据");
    }

    /**
     * 获取商品图片详细信息
     */
    @PreAuthorize("@ss.hasPermi('mxerp:productimg:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mxProductImgService.selectMxProductImgById(id));
    }


    @PreAuthorize("@ss.hasPermi('mxerp:productimg:query')")
    @GetMapping(value = "/getProductListimg/{id}")
    public AjaxResult getProductListimg(@PathVariable("id") Long id) {
        JSONObject rJson = new JSONObject();
        List<JSONObject> olist = mxProductImgMapper.getOneImgList(id + "", "0");
        List<JSONObject> tlist = mxProductImgMapper.getOneImgList(id + "", "1");
        rJson.put("olist",olist);
        rJson.put("tlist",tlist);
        return success(rJson);
    }



    /**
     * 新增商品图片
     */
    @PreAuthorize("@ss.hasPermi('mxerp:productimg:add')")
    @Log(title = "商品图片", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MxProductImg mxProductImg) {
        return toAjax(mxProductImgService.insertMxProductImg(mxProductImg));
    }

    /**
     * 修改商品图片
     */
    @PreAuthorize("@ss.hasPermi('mxerp:productimg:edit')")
    @Log(title = "商品图片", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MxProductImg mxProductImg) {
        return toAjax(mxProductImgService.updateMxProductImg(mxProductImg));
    }

    /**
     * 删除商品图片
     */
    @PreAuthorize("@ss.hasPermi('mxerp:productimg:remove')")
    @Log(title = "商品图片", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mxProductImgService.deleteMxProductImgByIds(ids));
    }
}
